<?php
/**
 * 	Plugin Content SyntaxHighlighter
 *
 * @package		SyntaxHighlighter
 * @copyright		Copyright (C) 2007 - 2011 Yoshiki Kozaki(www.joomler.net) All rights reserved.
 * @license		GNU General Public License version 2 or later
 */
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgContentSyntaxHighlighter extends JPlugin{

	public function onContentPrepare($context, &$article, &$params, $limitstart)
	{
		static $load = null;

		$tagName = $this->params->get('tagName', 'pre');

		if($this->params->get('customTag', 1) != 'default'){
			$matches = array();
			preg_match_all('/\{code\s([^\}]+?)\}(.+?)\{\/code\}/is', $article->text, $matches);
			if(count($matches[0])){
				for($i = 0, $total = count($matches[0]); $i < $total; $i++){
					$base = $matches[0][$i];
					$replace = '<'. $tagName. ' class="'. $matches[1][$i]. '">%s</'. $tagName.'>';
					$content = str_replace(array('&lt;br&gt;', '<br>', '<br />'), "\n", $matches[2][$i]);
					$content = sprintf($replace, $content);
					$article->text = str_replace($base, $content, $article->text);
				}
			}
		}

		if(!is_null($load)){
			return;
		}

		if(strpos($article->text, '<'. $tagName) === false){
			return;
		}

		if(strpos($article->text, 'class="brush:') === false){
			return;
		}

		$load = true;

		//SyntaxHighlighter.config
		//bloggerMode... This is Joomla!
		//strings
		$config = '';
		$temp = array();

		if($this->params->get('strings_loadlanguage') != 'default'){
			JFactory::getLanguage()->load('plg_content_syntaxhighlighter', JPATH_ADMINISTRATOR);
			$temp[] = 'var c = SyntaxHighlighter.config;';
			$temp[] = "c.strings.expandSource='". JText::_('PLG_CONTENT_SHL_EXPANDSOURCE', true). "';";
			$temp[] = "c.strings.help='". JText::_('PLG_CONTENT_SHL_HELP', true). "';";
			$temp[] = "c.strings.alert='". JText::_('PLG_CONTENT_SHL_ALERT', true). "';";
			$temp[] = "c.strings.noBrush='". JText::_('PLG_CONTENT_SHL_NOBRUSH', true). "';";
			$temp[] = "c.strings.brushNotHtmlScript='". JText::_('PLG_CONTENT_SHL_BRUSHNOTHTMLSCRIPT', true). "';";
		}

		if($this->params->get('stripBrs') != 'default'){
			$temp[] = 'c.stripBrs=true;';
		}

		if($tagName != 'pre'){
			$temp[] = "c.tagName='$tagName'";
		}

		if(count($temp)){
			$config = 'var c = SyntaxHighlighter.config;'. implode('', $temp);
		}

		//SyntaxHighlighter.defaults
		$default_params = array(
			'auto-links',
			'class-name',
			'collapse',
			'gutter',
			'html-script',
			'tab-size',
			'toolbar'
		);

		$temp = array();

		foreach($default_params as $param){
			$value = $this->params->get($param);
			if($value != 'default' && !empty($value)){
				if(!in_array($value, array('true', 'false')) && !is_numeric($value)) $value = "'$value'";
				$temp[] = "'$param':$value";
			}
		}

		$options = '{}';
		if(count($temp)){
			$options = '{'. implode(',', $temp). '}';
		}

		$base = 'plugins/content/syntaxhighlighter/';
		JHtml::_('stylesheet', $base. 'styles/shCore.css');
		JHtml::_('stylesheet', $base. 'styles/'. $this->params->get('display_style', 'shThemeDefault.css'));
		$base .='js/';
		JHtml::_('script', $base. 'shCore.js');
		$js = 'shAutoloaderEx';
		if(JDEBUG) $js .= '-uncompressed';
		JHtml::_('script', $base. $js. '.js');

		$base = "'". JURI::base(true). '/'.$base. "'";

		JHtml::_('behavior.framework');
		$js = "window.addEvent('domready',function(){ $config SyntaxHighlighter.autoloader($options, $base);});";
		JFactory::getDocument()->addScriptDeclaration($js);
	}
}